package com.gaattc.service;

import com.gaattc.dao.GoodsDao;
import com.gaattc.dao.GoodsDaoImpl;
import com.gaattc.model.Goods;
import com.gaattc.model.GoodsSpecs;
import com.gaattc.model.User;
import com.gaattc.model.bo.*;
import com.gaattc.model.vo.*;

import java.util.ArrayList;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<GoodsTypeVO> getType() {
        return goodsDao.getType();
    }

    @Override
    public List<GetGoodsByTypeVO> getGoodsByType(int typeId) {
        if(typeId != -1) {
            return goodsDao.getGoodsByType(typeId);
        }
        return goodsDao.getAllGoodsByType();
    }

    @Override
    public int addType(AddTypeBO addTypeBO) {
        return goodsDao.addType(addTypeBO);
    }

    @Override
    public int deleteType(int typeId) {
        return goodsDao.deleteType(typeId);
    }

    @Override
    public int deleteGoods(int id) {
        return goodsDao.deleteGoods(id);
    }

    @Override
    public void addGoods(AddGoodsBO addGoodsBO) {
        // TODO 事务
        List<AddSpecsBO> specList = addGoodsBO.getSpecList();

        //传参
        Goods goods = new Goods();
        goods.setImg(addGoodsBO.getImg());
        goods.setName(addGoodsBO.getName());
        goods.setDesc(addGoodsBO.getDesc());
        goods.setTypeId(addGoodsBO.getTypeId());

        goodsDao.addGoods(goods);
        //goods表插入之后，会生成一个id值，这个id值需要在规格报表中用到
        //   select last_insert_id()
        //即便其他的线程访问该数据库，然后插入数据，那么拿到的仍然是你的插入id
        //线程隔离的 只会取到当前线程的最后一次插入id
        int goodsId = goodsDao.getLastId();
        //通过goodsId添加规格信息
        goodsDao.addSpecs(specList, goodsId);
    }

    @Override
    public GetGoodsInfoVO getGoodsInfo(int id) {
        GetGoodsInfoVO getGoodsInfoVO = new GetGoodsInfoVO();
        getGoodsInfoVO.setGoods(goodsDao.getGoodsInfo_Goods(id));
        getGoodsInfoVO.setSpecs(goodsDao.getGoodsInfo_Specs(id));
        return getGoodsInfoVO;
    }

    @Override
    public UserGetGoodsInfoVO userGetGoodsInfo(int id) {
        UserGetGoodsInfoVO userGetGoodsInfoVO = goodsDao.userGetGoodsInfo(id);
        userGetGoodsInfoVO.setSpecs(goodsDao.getGoodsInfo_Specs(id));
        return userGetGoodsInfoVO;
    }

    @Override
    public void updateGoods(UpdateGoodsBO updateGoodsBO) {

        List<GoodsSpecs> specList = updateGoodsBO.getSpecList();

        Goods goods = new Goods();
        goods.setName(updateGoodsBO.getName());
        goods.setImg(updateGoodsBO.getImg());
        goods.setTypeId(updateGoodsBO.getTypeId());
        goods.setDesc(updateGoodsBO.getDesc());
        goods.setId(updateGoodsBO.getId());

        //删除再添加 == 更新
        goodsDao.deleteGoods(updateGoodsBO.getId());
        goodsDao.addGoods(goods);
        goodsDao.addSpecs(specList);
    }

    @Override
    public void addSpec(GoodsSpecs goodsSpecs) {
        //包装进List方便复用代码
        List<GoodsSpecs> list = new ArrayList<>();
        list.add(goodsSpecs);
        goodsDao.addSpecs(list);
    }

    @Override
    public void deleteSpec(GoodsSpecs goodsSpecs) {
        goodsDao.deleteSpec(goodsSpecs);
    }

    @Override
    public List<MsgVO> msg(int state) {
        return goodsDao.msg(state);
    }

    @Override
    public void reply(MsgVO msgVO) {
        goodsDao.reply(msgVO);
    }

    @Override
    public List<GetGoodsMsgVO> getGoodsMsg(int id) {
        return goodsDao.getGoodsMsg(id);
    }

    @Override
    public GetGoodsCommentVO getGoodsComment(int goodsId) {
        GetGoodsCommentVO getGoodsCommentVO = new GetGoodsCommentVO();

        List<CommentVO> list = goodsDao.getGoodsComment(goodsId);
        getGoodsCommentVO.setCommentList(list);
        Double avgScore = goodsDao.getAvgScore(goodsId);
        getGoodsCommentVO.setRate(avgScore);

        return getGoodsCommentVO;
    }

    @Override
    public List<GetGoodsByTypeVO> searchGoods(String keyword) {
        return goodsDao.searchGoods(keyword);
    }

    @Override
    public void askGoodsMsg(AskGoodsMsgBO askGoodsMsgBO) {
        int userId = goodsDao.getUser(askGoodsMsgBO.getToken()).getId();
        goodsDao.askGoodsMsg(askGoodsMsgBO, userId);
    }

}
